<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button> -->
	  <!-- 搜索板块 -->
	  <a-row class="row-item-search">
	    <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
	      <a-form-item label="订单查询">
	        <a-input style="width: 337px" placeholder="请输入关键词" v-decorator="['searchValue']">
	        </a-input>
	      </a-form-item>
	      <a-form-item label="发放状态">
	        <a-select v-decorator="['is_grant', { initialValue: -1 }]">
	          <a-select-option :value="-1">全部</a-select-option>
	          <a-select-option
	            :value="1"
	          >已发放</a-select-option>
	      			  <a-select-option
	      			    :value="0"
	      			  >未发放</a-select-option>
	        </a-select>
	      </a-form-item>
	      <a-form-item label="审核状态">
	        <a-select v-decorator="['is_auth', { initialValue: -1 }]">
	          <a-select-option :value="-1">全部</a-select-option>
	          <a-select-option
	            :value="1"
	          >已审核</a-select-option>
			  <a-select-option
			    :value="0"
			  >未审核</a-select-option>
	        </a-select>
	      </a-form-item>
	      <a-form-item label="结算时间">
	        <a-month-picker format="YYYY-MM" v-decorator="['month', { initialValue: defaultDate }]"  style="width: 336px;" />
	      </a-form-item>
	      <a-form-item class="search-btn">
	        <a-button class="mr-15" type="primary" icon="search" html-type="submit">搜索</a-button>
	        <a-button @click="handleReset">重置</a-button>
	      </a-form-item>
	    </a-form>
	  </a-row>
    </div>
    <s-table
      ref="table"
      rowKey="reward_id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >
      <span slot="grant_time" slot-scope="text, item">
        <a-tag v-if="item.grant_time">{{item.grant_time}}</a-tag>
      </span>
	  <span slot="is_auth" slot-scope="text, item">
	    <a-tag v-if="text > 0" color="green">已审核</a-tag>
	  		<a-tag v-else >未审核</a-tag>
	  </span>
	  <span slot="is_grant" slot-scope="text, item">
	    <a-tag v-if="text > 0" color="green">已发放</a-tag>
	  		<a-tag v-else >未发放</a-tag>
	  </span>
      <!-- 操作 -->
      <span slot="action" slot-scope="text, item">
        <a v-if="item.is_grant == 0" v-action:edit style="margin-right: 8px;" @click="handleAuth(item)">审核</a>
      </span>
    </s-table>
    <AuthForm ref="AuthForm" @handleSubmit="handleRefresh" />
    <EditForm ref="EditForm" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import moment from 'moment'
import * as Api from '@/api/user/kpi'
import { STable } from '@/components'
import { AuthForm, EditForm } from './modules'

export default {
  name: 'Index',
  components: {
    STable,
    AuthForm,
    EditForm
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '记录ID',
          dataIndex: 'reward_id'
        },
        {
          title: '用户名称',
          dataIndex: 'name'
        },
		{
		  title: '等级',
		  dataIndex: 'grade_name'
		},
        {
          title: '当月累计充值金额',
          dataIndex: 'recharge_money'
        },
        {
          title: '奖励比例',
          dataIndex: 'reward_ratio'
        },
		{
		  title: '奖励金额',
		  dataIndex: 'reward_money',
		},
        {
          title: '结算时间',
          dataIndex: 'settlement_time',
        },
        // {
        //   title: '等级权益',
        //   dataIndex: 'equity',
        //   scopedSlots: { customRender: 'equity' }
        // },
        {
          title: '发放时间',
          dataIndex: 'grant_time',
		  scopedSlots: { customRender: 'grant_time' }
        },
		{
		  title: '审核',
		  dataIndex: 'is_auth',
		  scopedSlots: { customRender: 'is_auth' }
		},
		{
		  title: '状态',
		  dataIndex: 'is_grant',
		  scopedSlots: { customRender: 'is_grant' }
		},
		{
		  title: '备注',
		  dataIndex: 'remark',
		},
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      },
	  searchForm: this.$form.createForm(this),
	  // 默认日期范围
	  defaultDate: moment()
	  
    }
  },
  created () {
	  const currentDate = new Date(); // 获取当前日期
	  // 获取当前月份的上一个月份
	  const prevMonth = currentDate.getMonth();
	  // 处理边界情况，如果当前月份为1月，上一个月应为12月
	  const month = prevMonth < 0 ? 11 : prevMonth;
	  // 获取上个月的年份
	  const year = currentDate.getFullYear();
	  const prevMonthDate = new Date(year, month, 0);
	  this.defaultDate = moment(prevMonthDate)
  },
  methods: {
	// 确认搜索
	handleSearch (e) {
	  e.preventDefault()
	  this.searchForm.validateFields((error, values) => {
	    if (!error) {
	      this.queryParam = { ...this.queryParam, ...values }
	      this.handleRefresh(true)
	    }
	  })
	},
	// 重置搜索表单
	handleReset () {
	  this.searchForm.resetFields()
	},
    /**
     * 新增记录
     */
    handleAuth (item) {
      this.$refs.AuthForm.add(item)
    },

    /**
     * 编辑记录
     */
    handleEdit (item) {
      this.$refs.EditForm.edit(item)
    },

    /**
     * 删除记录
     */
    handleDelete (item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk () {
          return Api.deleted({ gradeId: item.grade_id })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => {
              modal.destroy()
            })
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    }
  }
}
</script>
